package com.atguigu.dga.meta.service.impl;

import com.atguigu.dga.constant.DgaConstant;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.mapper.TableMetaInfoExtraMapper;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author wuzhida
 * @since 2024-06-24
 */
@Service
@DS("dga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {

    /**
     * 初始化表的辅助信息， 每张表只对应一条辅助信息， 因此每张表只需要初始化一次辅助信息
     *
     * 判断每张表是否被初始化过， 如果之前已经初始化过， 不再进行初始化， 如果没有被初始化，则进行初始化。
     *
     * 通过判断当前表是否已经在 table_meta_info_extra表中存在即可了解到是否已经被初始化过。
     *
     * @param tableMetaInfosList  待初始化的表
     */
    @Override
    public void initTableMetaInfoExtra(List<TableMetaInfo> tableMetaInfosList) {
        List<TableMetaInfoExtra> tableMetaInfoExtras = new ArrayList<>(tableMetaInfosList.size());
        for (TableMetaInfo tableMetaInfo : tableMetaInfosList) {
            TableMetaInfoExtra metaInfoExtra = getOne(
                    new QueryWrapper<TableMetaInfoExtra>()
                            .eq("schema_name", tableMetaInfo.getSchemaName())
                            .eq("table_name", tableMetaInfo.getTableName())
            );
            //如果不存在辅助信息则存
            if (metaInfoExtra == null) {
                TableMetaInfoExtra tableMetaInfoExtra = new TableMetaInfoExtra();
                tableMetaInfoExtra.setTableName(tableMetaInfo.getTableName());
                tableMetaInfoExtra.setSchemaName(tableMetaInfo.getSchemaName());
                tableMetaInfoExtra.setTecOwnerUserName(DgaConstant.TEC_OWNER_USER);
                tableMetaInfoExtra.setBusiOwnerUserName(DgaConstant.BUSI_OWNER_USER);
                tableMetaInfoExtra.setLifecycleType(DgaConstant.LIFECYCLE_TYPE_UNSET);
                tableMetaInfoExtra.setLifecycleDays(-1L);
                tableMetaInfoExtra.setSecurityLevel(DgaConstant.SECURITY_LEVEL_UNSET);
                tableMetaInfoExtra.setDwLevel(getDwLevel(tableMetaInfo.getTableName()));
                tableMetaInfoExtra.setCreateTime(new Date());

                //攒批
                tableMetaInfoExtras.add(tableMetaInfoExtra);
            }
        }
        saveBatch(tableMetaInfoExtras);
    }

    /**
     * 通过表名, 确定所属的数仓层级
     * @param tableName 表名
     * @return
     */
    private String getDwLevel(String tableName) {
        String res;
        if (tableName.startsWith("ods")){
            res = "ODS";
        } else if (tableName.startsWith("dim")){
            res = "DIM";
        } else if (tableName.startsWith("dwd")){
            res = "DWD";
        } else if (tableName.startsWith("dws")){
            res = "DWS";
        } else if (tableName.startsWith("ads")){
            res = "ADS";
        } else if (tableName.startsWith("dm")){
            res = "DM";
        } else {
            res = "OTHER";
        }
        return res;
    }
}
